<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
/**
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');


class NewsController extends AdminbaseController{
	
	protected $users_model,$role_model;
	
    public function _initialize() {
        parent::_initialize();
        $this->role_model = D("Common/Role");
		$this->users_model = D("Common/Users");
    }

    public function index(){//公告管理
    	$where=array();
    	$request=I('request.');
    	if($request['classid'] !=0){
    		$where['ClassID']=$request['classid'];
    		$this->assign("classid",$request['classid']);
    	}
    	if(!empty($request['subject'])){
    		$where['Subject']=$request['subject'];
    		$this->assign("subject",$request['subject']);
    	}
    	if(in_array($request['islock'], $request)){
    		$where['IsLock']=$request['islock'];
    		if($request['islock'] === "")unset($where['IsLock']);
    		$this->assign("islock",$request['islock']);
    	}
    	$start_time=I('request.start_time');
    	if(!empty($start_time)){
    		$where['IssueDate'][0]=array(
    				array('EGT',$start_time)
    		);
    		$this->assign("start_time",$request['start_time']);
    	}
    	$end_time=I('request.end_time');
    	if(!empty($end_time)){
    		$where['IssueDate'][1]=array(
    				array('ELT',$end_time)
    		);
    		$this->assign("end_time",$request['end_time']);
    	}
    	$count = M('News')
    	->where($where)->count();
    	$size = $page = $this->page($count, 25);
    	
    	$data = M('News')
    	->table('sp_news n')
    	->join("sp_users u on n.userid = u.id")
    	->field("n.*,u.user_login")
    	->where($where)
    	->order("newsid DESC")
    	->limit($page->firstRow, $page->listRows)
    	->select();
    	if(!empty($data) && is_array($data)){
    		$existdata=true;
    	}else{
    		$existdata=false;
    	}
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("news", $data);
    	$this->assign("existdata", $existdata);
    	$this->display();
    }
    public function add(){//添加公告
    	$this->display();
    }
    public function add_post(){
    	if (IS_POST) {
    		if(empty($_POST['frm']['ClassID'])){
    			$this->error("请选择一个分类！");
    		}
    		if(!empty($_POST['frm']['thumb'])){
    			$_POST['frm']['thumb'] = sp_asset_relative_url($_POST['frm']['thumb']);
    		}
    		$_POST['frm']['IssueDate']=date("Y-m-d H:i:s",time());
    		$_POST['frm']['UserID']=get_current_admin_id();
    		$_POST['frm']['IssueIP']=get_client_ip();
    		$news=I("post.frm");
    		$news['FormattedBody']=htmlspecialchars_decode($news['FormattedBody']);
    		if (M("News")->add($news)!==false) {
    			$this->success("添加成功",U("News/index"));
    		} else {
    			$this->error("添加失败！");
    		}
    	}
    }
    public function edit(){//添加公告
    	$id = I('get.id');
    	$item = M("News")
    	->where("newsid='$id'")
    	->find();
    	$this->assign("news", $item);
    	$this->display();
    }
    public function edit_post(){
    	if (IS_POST) {
    		$id = I('post.id');
    		if(!empty($_POST['frm']['thumb'])){
    			$_POST['frm']['thumb'] = sp_asset_relative_url($_POST['frm']['thumb']);
    		}
    		$_POST['frm']['LastModifyDate']=date("Y-m-d H:i:s",time());
    		$_POST['frm']['LastModifyIP']=get_client_ip();
    		$news=I("post.frm");
    		$news['FormattedBody']=htmlspecialchars_decode($news['FormattedBody']);
    		$item = M("News")->where(array("NewsID"=>$id));
    		if ($item->save($news)!==false) {
    			$this->success("编辑成功",U("News/index"));
    		} else {
    			$this->error("编辑失败！");
    		}
    	}
    }
    public function check(){
    	if(isset($_POST['ids'])){
    		$ids = join( ',', $_POST[ 'ids' ] );
    		$data=$_GET;
    		if (M("News")->where(" newsid in ($ids)")->save($data)) {
    			$this->success("操作成功！");
    		} else {
    			$this->error("操作失败！");
    		}
    	}
    }
    public function delete(){
    	if(isset($_POST['ids'])){
    		$ids = join( ',', $_POST[ 'ids' ] );
    		if (M("News")->where(" newsid in ($ids)")->delete()) {
    			$this->success("删除成功！");
    		} else {
    			$this->error("删除失败！");
    		}
    	}else{
    		$id = $_GET[ 'id' ];
    		if (M("News")->where(" newsid = $id")->delete()) {
    			$this->success("删除成功！");
    		} else {
    			$this->error("删除失败！");
    		}
    	}
    }
}//结束
?>